package com.elinkway.infinitemovies.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.elinkway.infinitemovies.async.MoviesHttpAsyncTask;
import com.elinkway.infinitemovies.bean.UserUpdateBean;
import com.elinkway.infinitemovies.http.api.MoviesHttpApi;
import com.elinkway.infinitemovies.http.parser.UserUpdateParser;
import com.elinkway.infinitemovies.utils.ToastUtil;
import com.le123.ysdq.R;
import com.lvideo.http.bean.LVideoDataHull;
import com.umeng.analytics.MobclickAgent;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NicknameModifyActivity extends BaseSecondaryActivity implements TextWatcher, View.OnClickListener{
    public static final String TAG = "NicknameModifyActivity";
    public final static String NICKNAME = "nickname";
    private final static int NICKNAME_SAMLL_LEN = 4;
    private String nickNameMatch = "^[\\u4E00-\\u9FA5A-Za-z0-9_]{4,32}+$";
    private Pattern mNickNamePattern;
    private Matcher mNickNameMatcher;
    private EditText mNickname;
    private ImageView mNicknameDelete;
    private RequestUserUpdateTask mRequestUserUpdateTask;
    private String newNickName;
    private String oldNickName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setActionBarActivityClassName(TAG);
        setContentView(R.layout.activity_nickname_modify);
        initActionBar();
        mTitleTv.setText(getResources().getString(R.string.nickname));
        initView();
    }

    private void initView() {
        mNickNamePattern = Pattern.compile(nickNameMatch);
        mNickname = (EditText) findViewById(R.id.nickname);
        mNicknameDelete = (ImageView) findViewById(R.id.nickname_delete);
        mNickname.addTextChangedListener(this);
        mNicknameDelete.setOnClickListener(this);
        oldNickName = getIntent().getStringExtra(NICKNAME).trim();
        mNickname.setText(oldNickName);
        mNickname.setSelection(oldNickName.length());
        mRightFirstTv.setText(getResources().getString(R.string.ok));
        mRightFirstTv.setVisibility(View.VISIBLE);
        mRightFirstTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                newNickName = mNickname.getText().toString().trim();
                if("".equals(newNickName)) {
                    ToastUtil.showShortToast(NicknameModifyActivity.this, getResources().getString(R.string.nickname_empty));
                    return;
                }
                if(oldNickName.equals(newNickName)) { //用户名没做修改
                    finish();
                    return;
                }
                if(isNicknameMatch(newNickName)) {
                    mRequestUserUpdateTask = new RequestUserUpdateTask(NicknameModifyActivity.this, newNickName);
                    mRequestUserUpdateTask.start();
                }else if(newNickName.length() < NICKNAME_SAMLL_LEN) {
                    ToastUtil.showShortToast(NicknameModifyActivity.this, getResources().getString(R.string.nickname_len_limit));
                }else {
                    ToastUtil.showShortToast(NicknameModifyActivity.this, getResources().getString(R.string.nickname_input_toast));
                }
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public static void launch(Context context) {
        Intent intent = new Intent(context, NicknameModifyActivity.class);
        context.startActivity(intent);
    }

    public boolean isNicknameMatch(String str) {
        mNickNameMatcher = mNickNamePattern.matcher(str);
        return mNickNameMatcher.matches();
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
    }

    @Override
    public void afterTextChanged(Editable editable) {


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.nickname_delete:
                mNickname.setText("");
                break;
        }
    }

    public class RequestUserUpdateTask extends MoviesHttpAsyncTask<UserUpdateBean> {
        private String nickname;

        @Override
        public void netNull() {
            super.netNull();
        }

        @Override
        public void netErr(int updateId, String errMsg) {
            super.netErr(updateId, errMsg);
        }

        @Override
        public void dataNull(int updateId, String errMsg) {
            super.dataNull(updateId, errMsg);
        }

        @Override
        public void codeErr(String code, String msg) {
            super.codeErr(code, msg);
            Toast.makeText(NicknameModifyActivity.this, msg, Toast.LENGTH_SHORT).show();
        }

        public RequestUserUpdateTask(Context context, String nickname) {
            super(context);
            this.nickname = nickname;
        }

        @Override
        public LVideoDataHull<UserUpdateBean> doInBackground() {
            LVideoDataHull<UserUpdateBean> ret = MoviesHttpApi.userUpdateRequest(
                    new UserUpdateParser(), nickname, "", "");
            if (ret.getDataType() == LVideoDataHull.DataType.DATA_IS_INTEGRITY) {
                UserUpdateBean v = ret.getDataEntity();

            }
            return ret;
        }

        @Override
        public void onPostExecute(int updateId, UserUpdateBean result) {
            Intent intent = new Intent();
            intent.putExtra(UserMessageActivity.NICKNAME, newNickName);
            NicknameModifyActivity.this.setResult(RESULT_OK, intent);
            finish();
        }
    }
}
